using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Security.Cryptography;
using System.IO;

namespace Pricom
{
  public class Config
  {
    public Config()
    {
    }

    public class Script
    {
      public Script()
      {
      }
      public Script(string _event, string _body) 
      {
        this.Event = _event;
        this.Body = _body;
      }
      public string Event;
      public string Body;
    }
    public class Connection
    {
      public Connection()
      {
      }
      public Connection(string nick, string server, string port, string channel)
      {
        //scripts.Add(new Script("OnNickClicked", "MessageBox.Show(\"ok\");"));
        //scripts.Add(new Script("OnValamiClicked", "MessageBox.Show(param[0].ToString());"));
        Nick = nick;
        Server = server;
        Port = port;
        Channel = channel;
      }
      public string Nick = "Newbie";
      public string Server = "irc.elte.hu";
      public string Port = "6667";
      public string Channel = "#Beginners";
      public string Proxy = string.Empty;
      public bool FlashOnMessage = true;
      public bool SaveLog = true;
      public List<Script> scripts = new List<Script>();
    }

    public class User
    {
      public RSAParameters rsaParams = new RSAParameters();
      public byte[] encryptTest;
    }
    public class Font
    {
      public string Face;
      public float Size;
      public System.Drawing.FontStyle Style;
    }
    public class Messages
    {
      public bool sendUTF8 = true;
      public bool fixUTF8bug152 = true;
    }
    public class Client
    {
      public bool minimizeOnEsc = true;
    }
    public class Colors
    {
      public System.Drawing.Color outputBackground = System.Drawing.Color.Empty;
      public System.Drawing.Color outputText = System.Drawing.Color.Empty;
      public System.Drawing.Color inputBackground = System.Drawing.Color.Empty;
      public System.Drawing.Color inputText = System.Drawing.Color.Empty;
      public System.Drawing.Color nicklistBackground = System.Drawing.Color.Empty;
      public System.Drawing.Color nicklistText = System.Drawing.Color.Empty;
    }

    //
    public User user = new User();
    public Font font = new Font();
    public Colors colors = new Colors();
    public Messages messages = new Messages();
    public Client client = new Client();
    public Connection defaultConnection = new Connection();
    public List<Connection> connections = new List<Connection>();
    //

    public string GetFileName()
    {
      return loadedFromFile;
    }

    public void Save()
    {
      if (loadedFromFile.Length == 0)
        return;
      SaveAs(loadedFromFile);
    }

    public void SaveAs(string file)
    {
      try
      {
        XmlSerializer serializer = new XmlSerializer(typeof(Config));
        StreamWriter writer = new StreamWriter(file);
        serializer.Serialize(writer, this);
      }
      catch
      {
      }
    }

    static public Config Load(string file)
    {
      Config config = new Config();
      config.loadedFromFile = file;
      if (!File.Exists(file))
        return config;
      try
      {
        XmlSerializer serializer = new XmlSerializer(typeof(Config));
        
        using (StreamReader reader = new StreamReader(file))
        {
          config = serializer.Deserialize(reader) as Config;
          config.loadedFromFile = file;
        }
        return config;
      }
      catch
      {
        return config;
      }
    }

    [XmlIgnore]
    private string loadedFromFile = string.Empty;
    [XmlIgnore]
    static public Config main = new Config();
  }
}
